﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using MSXML2;
public partial class CshapandJsASC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //返回汉字 用法
        Response.Write( CodingToCharacter("59713456")+"<br/>");
        //返回 用法
        Response.Write(CodeText("q")+"<br/>");

        //得到一个字符的ASCII码 方式１
        cc.InnerText = ((Int16)((char)'b')).ToString();
        //得到一个字符的ASCII码 方式２
        cc.InnerText += ((Int32)'a').ToString();
        //得到一个字符的ASCII码 方式3
        Char c='c';
        cc.InnerText += ((int)c).ToString();
        //多字符操作 unicode to ascii
       //ASCII to unicode
        /////////////////////////////////

        string str = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes("aaaadfsdf".ToCharArray()));

        //unicode to ascii
        ///////////////////////////////////
        char[] chs = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes("dsfs".ToCharArray()));
        for(int i=0;   i<chs.Length;i++)
        {
            cc.InnerText += "&#" + ((int)chs[i]).ToString()+";";
        }
        ///// 

        string body="",link="",html="",strx="",url="http://search.17173.com/bk.jsp?keyword=aa&submit=";
        str = Request.QueryString.ToString();
        if (str != "")
        {
            url = "http://search.17173.com/bk.jsp?"+str;
            url = Server.UrlDecode(url);
        }
        Response.Write(url);
        //新建xmlhttp对象
        MSXML2.XMLHTTP xmlhttp = new XMLHTTP();
        //xmlhttp.open("get", "http://www.baidu.com/s?wd=aa&cl=3", false, "", "");
        xmlhttp.open("get",url,false,"","");
        xmlhttp.setRequestHeader("Content-Type","text/html; charset=gb2312");
        xmlhttp.send(null);
        html=xmlhttp.responseText.ToString().Replace("/bk.jsp","/xml/Net_Ajax.aspx");//得到数据
        body = html.Substring(html.IndexOf("<BODY>"), (html.IndexOf("</BODY>") - html.IndexOf("<BODY>")));//从ＢＯＤＹ开始的地方截取　到ＢＯＤＹ结束
        link = html.Substring(0,html.IndexOf("<BODY>")).ToString();
        link = link.Substring(link.IndexOf("<link href="),link.LastIndexOf("type=\"text/css\""));
       
       // cont.InnerHtml =body ;
        cc.InnerHtml=CodeText(body);

    }

    //方法1
    /// <summary>
    /// 传入汉字的ASC||码 返回汉字 
    /// </summary>
    /// <param name="coding"> 传入ASC||码 必须为4的倍数 Ex:5971</param>
    /// <![CDATA[ CodingToCharacter("59713456")  ]]>
    /// <returns>此ASC码的 返回汉字 </returns>
    public string CodingToCharacter(string coding)
    {
        string characters = "";
        if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。   
        {
            throw new System.Exception("编码格式不正确");
        }
        for (int i = 0; i < coding.Length; i += 4)   //每四位为一个汉字   
        {
            byte[] bytes = new byte[2];
            string lowCode = coding.Substring(i, 2);   //取出低字节,并以16进制进制转换   
            bytes[0] = System.Convert.ToByte(lowCode, 16);
            string highCode = coding.Substring(i + 2, 2);   //取出高字节,并以16进制进行转换   
            bytes[1] = System.Convert.ToByte(highCode, 16);
            string character = System.Text.Encoding.Unicode.GetString(bytes);
            characters += character;
        }
        return characters;
    }

    /// <summary>
    /// 传入字符串 返回unicode编码
    /// </summary>
    /// <param name="tt">传入字符串</param>
    /// <returns>返回unicode编码</returns>
    public String CodeText(String tt)
    {
        char[] chs = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes(tt.ToCharArray()));
        for (int i = 0; i < chs.Length; i++)
        {
            tt += "&#" + ((int)chs[i]).ToString() + ";";
        }



        /*
        C#里面的字符串常量都是Unicode

        如果你需要Unicode字节：

        string test = "你好";

        byte[] testBytes = System.Text.Encoding.Unicode.GetBytes( test );

        字符：

        char c = 'A';

        int asciiOfA = (int)c;  //asciiOfA 就是 'A' 的ASCII码
         ////////////////////////////////////
        可以如下转换

        ASCII to unicode
        /////////////////////////////////
        System.IO.FileStream fs = new FileStream("F:\\test.txt",FileMode.Open );
        byte[] bt = new Byte[fs.Length];
        fs.Read(bt,0,bt.Length);
        Encoding CnEnconding	= Encoding.GetEncoding("GB18030");
        string str = CnEnconding.GetString(bt);

        unicode to ascii
        ///////////////////////////////////
        char[] chs = CnEnconding.GetChars("斯大林开发加速度");

         * 
         * 
         * 

        至于你的题目,大家说了这么多,你都没有听:
        string sHello	= "你好";	// unicode 编码了

        估计你是想加密吧,

        以下的方法,不知可用否,请试试:
        ================================================
        string sSou				= "用System.text 命名空间";
        Encoding CnEnconding	= Encoding.GetEncoding("GB18030");

        byte[]	bSou			= CnEnconding.GetBytes(sSou);		//转成数组

        string sUni				= Encoding.Unicode.GetString(bSou);

        //保存


        //取出

        //回复时 反操作
        byte[]	bUni2			= Encoding.Unicode.GetBytes(sUni);		//转成数组

        string	sDest			= CnEnconding.GetString(bUni2);


         */

        return tt;

    }
    
}
